package com.cs.common.utils;

import java.util.Arrays;
import java.util.List;

import com.cs.common.init.SystemParameterCollection;
import com.cs.common.init.beans.SystemParameterBean;

public class FileExtensionUtils {

	private static FileExtensionUtils instance;
	
	private List allowedFileExtensions;
	
	private static final String ALLOWED_FILE_EXTENSIONS = "ALLOWED_FILE_EXTENSIONS";
	
	/**
	 * Default constructior	 
	 */
	private FileExtensionUtils(){
		//allowedFileExtensions = new ArrayList();
		String[] aux;
		SystemParameterBean param = SystemParameterCollection.getInstance().getSystemParameterBean(ALLOWED_FILE_EXTENSIONS);
		aux = param.getValue().split(",");
		allowedFileExtensions = Arrays.asList(aux);		
	}
	
	/**
	 * Instacia de la clase
	 * @return
	 */
	private static FileExtensionUtils getInstance(){
		if (instance == null){
			instance = new FileExtensionUtils();
		}
		return instance;
	}
	
	public static boolean isValidExtension(String ext){
		return FileExtensionUtils.getInstance().allowedFileExtensions.contains(ext.toLowerCase());
	}
	
	public static String getFileExtension(String fileName){
		String fileExtension = "";
		int index = fileName.lastIndexOf(".");
		if(index!=-1){
			fileExtension = fileName.substring(index + 1, fileName.length());
		}
		return fileExtension;

	}
}
